<?php
class LinksController extends AppController {

	var $name = 'Links';

	var $uses = array('Link', 'Tab');

	private function index() {
		$this->Link->recursive = 0;
		$this->set('links', $this->paginate());
	}

	private function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid link', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('link', $this->Link->read(null, $id));
	}

	function add() {
		$this->layout = '';
		if (!empty($this->data)) {
			$tab = $this->Tab->find('first', array('conditions' => array(
					'Tab.user_id' => $this->Auth->user('id'),
					'Tab.position' => $this->data['Link']['tab_id']
			)));

			$this->data['Link']['tab_id'] = $tab['Tab']['id'];

			$this->Link->create();
			if ($this->Link->save($this->data)) {
				$links = $this->Link->find('all', array(
						'conditions' => array('tab_id' => $tab['Tab']['id']),
						'order' => 'Link.created DESC'));
				$this->set('code', 'success');
				$this->set(compact('links', 'tab'));
			} else {
				$this->set('code', 'error');
				$this->set('errors', $this->Link->validationErrors);
			}
		} else {
			$this->redirect('/');
		}
	}

	private function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid link', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Link->save($this->data)) {
				$this->Session->setFlash(__('The link has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The link could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Link->read(null, $id);
		}
		$tabs = $this->Link->Tab->find('list');
		$this->set(compact('tabs'));
	}

	function delete($id, $tab_id) {
		$user_tabs = $this->get_user_tabs('id');
		$link = $this->Link->find('first', array('conditions' => array('Link.id' => $id)));
		if (in_array($link['Link']['tab_id'], $user_tabs)) {
			$this->Link->delete($id);
			$this->redirect(array('controller' => 'tabs', 'action' => 'index', $tab_id));
		}
		$this->redirect('/');
	}
	function admin_index() {
		$this->Link->recursive = 0;
		$this->set('links', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid link', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('link', $this->Link->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Link->create();
			if ($this->Link->save($this->data)) {
				$this->Session->setFlash(__('The link has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The link could not be saved. Please, try again.', true));
			}
		}
		$tabs = $this->Link->Tab->find('list');
		$this->set(compact('tabs'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid link', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Link->save($this->data)) {
				$this->Session->setFlash(__('The link has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The link could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Link->read(null, $id);
		}
		$tabs = $this->Link->Tab->find('list');
		$this->set(compact('tabs'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for link', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Link->delete($id)) {
			$this->Session->setFlash(__('Link deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Link was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>